package com.hmsc.api.service;

import com.alibaba.fastjson.JSON;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmall.common.dto.ItemDTO;

import com.hmsc.api.ItemDoc;
import com.hmsc.api.service.ItemSearchService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class ItemSearchServiceImpl implements ItemSearchService {
//    @Autowired
    private RestHighLevelClient client =
    new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://192.168.138.100:9200")));

       /**
     * 批量导入
     * @param shop
        */
    public void importshop(List<ItemDTO> shop) {
        BulkRequest bulkRequest = new BulkRequest();



        for (ItemDTO item : shop) {
            IndexRequest indexRequest = new IndexRequest("shop").id(item.getId().toString());
            ItemDoc itemDoc = new ItemDoc(item);
            indexRequest.source(JSON.toJSONString(itemDoc), XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        try {
           client.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
